package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcEpisodeMapper;
import com.dhcc.cdc.busi.dao.CdcNurMapper;
import com.dhcc.cdc.busi.dao.CdcUserMapper;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.entity.CdcNurEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcNurService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 护理病历表数据服务层接口
 * 
 * @ClassName: CdcNurServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Service("cdcNurService")
public class CdcNurServiceImpl extends BaseServiceImpl<CdcNurMapper, CdcNurEntity> implements ICdcNurService {
	@Autowired
	private CdcEpisodeMapper cdcEpisodeMapper;
	
	@Autowired
	private CdcUserMapper cdcUserMapper;
	
	@Autowired
	private CdcDataMapper cdcDataMapper;
	
	@Override
	public List<CdcNurEntity> page(Page<CdcNurEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcNurEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}
	@Override
	public String handleXmlData(String dataStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            Iterator<Element> iter = rootElt.elementIterator("nur"); // 获取根节点下的子节点emr
            // 遍历emr节点
            while (iter.hasNext()) {
                Element emrEle = iter.next();
                String xcode = emrEle.elementTextTrim("xcode"); //索引码
                String episodeid = emrEle.elementTextTrim("episodeid"); //就诊记录
                String description = emrEle.elementTextTrim("description"); //描述
                String content = emrEle.elementTextTrim("content"); //内容
                String nurdatestr = emrEle.elementTextTrim("nurdate"); //填写日期
                String buruserid = emrEle.elementTextTrim("buruserid"); //填写人
                String updatetimestr = emrEle.elementTextTrim("updatetime"); //更新时间
                String updateuserid = emrEle.elementTextTrim("updateuserid"); //更新人
                String opercode = emrEle.elementTextTrim("opercode"); //操作码
                CdcNurEntity nur = new CdcNurEntity();
                nur.setXcode(xcode);
            	//获取dataid
            	CdcDataEntity cdcDataEntity = new CdcDataEntity();
            	cdcDataEntity.setCode("NurData");
            	cdcDataEntity.setIsActive(1);
            	cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
        		Long dataId = cdcDataEntity.getId();
        		//获取basedataId
    			CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
    			cdcDataEntityBase.setCode("HisBase");
    			cdcDataEntityBase.setIsActive(1);
    			cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
        		Long basedataId = cdcDataEntityBase.getId();
        		if(CommonUtil.isNotEmpty(updatetimestr)) {
            		Date updatetime = sdf.parse(updatetimestr);
            		nur.setUpdateDate(updatetime);
            	}
            	//获取更新人
            	if(CommonUtil.isNotEmpty(updateuserid)) {
            		CdcUserEntity cdcUserEntity = new CdcUserEntity();
            		cdcUserEntity.setXcode(updateuserid);
            		cdcUserEntity.setDataId(basedataId);
            		cdcUserEntity.setIsActive(1);
            		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
            		//判断人员存在不存在
            		Long updateuser = null;
            		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
            			cdcUserEntity = new CdcUserEntity();
            			cdcUserEntity.setXcode(updateuserid);
                		cdcUserEntity.setIsActive(1);
            			cdcUserEntity.setCode("newuser"+updateuserid);
            			cdcUserEntity.setName("newuser"+updateuserid);
                		cdcUserEntity.setDataId(basedataId);
                		//保存用户
                		cdcUserMapper.insert(cdcUserEntity);
                		updateuser = cdcUserEntity.getId();
            		}else {
            			updateuser = cdcUserEntity.getId();
            		}
            		nur.setUpdateBy(updateuser);
            	}
                if("1".equals(opercode)) {//保存/修改
                	//获取episodeid
                	CdcEpisodeEntity cdcEpisodeEntity = new CdcEpisodeEntity();
                	cdcEpisodeEntity.setXcode(episodeid);
                	cdcEpisodeEntity=cdcEpisodeMapper.selectOne(cdcEpisodeEntity);//就诊信息没在库中？？？
                	Long epsid = cdcEpisodeEntity.getId();
                	nur.setEpisodeId(epsid);
                	if(CommonUtil.isNotEmpty(description)) {
                		nur.setDescription(description);
                	}
                	if(CommonUtil.isNotEmpty(content)) {
                		nur.setContent(content);
                	}
                	if(CommonUtil.isNotEmpty(nurdatestr)) {
                		Date nurdate = sdf.parse(nurdatestr);
                		nur.setNurDate(nurdate);
                	}
                	//获取填写人
                	if(CommonUtil.isNotEmpty(buruserid)) {
                		CdcUserEntity cdcUserEntity = new CdcUserEntity();
                		cdcUserEntity.setXcode(buruserid);
                		cdcUserEntity.setDataId(basedataId);
                		cdcUserEntity.setIsActive(1);
                		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
                		//判断人员存在不存在
                		Long nuruserlongid = null;
                		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
                			cdcUserEntity = new CdcUserEntity();
                			cdcUserEntity.setXcode(buruserid);
                    		cdcUserEntity.setIsActive(1);
                			cdcUserEntity.setCode("newuser"+buruserid);
                			cdcUserEntity.setName("newuser"+buruserid);
                    		cdcUserEntity.setDataId(basedataId);
                    		//保存用户
                    		cdcUserMapper.insert(cdcUserEntity);
                    		nuruserlongid = cdcUserEntity.getId();
                		}else {
                			nuruserlongid = cdcUserEntity.getId();
                		}
                		nur.setBurUserId(nuruserlongid);
                	}
                	nur.setDataId(dataId);
            		nur.setIsActive(1);
                }else {
                	nur.setIsActive(0);
                }
                //先判断是不是有该对象
                Wrapper<CdcNurEntity> wrapper =  new EntityWrapper<CdcNurEntity>();
                wrapper.eq("xcode", nur.getXcode());
                wrapper.eq("data_id", dataId);
                int count = this.selectCount(wrapper);
                if(count>0) {
                	this.update(nur, wrapper);
                }else {
                	this.insert(nur);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        }
		return "<response><status>1</status><message>ok</message></response>";
	
	}

}